package com.hulk.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.omg.CORBA.SystemException;
import org.springframework.jdbc.core.JdbcTemplate;

public class DateUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	public final static String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private final static Calendar utilCalendar = Calendar.getInstance();

	public static Date stringToDate(String dateStr, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			throw new Exception(e.getMessage());
		}
	}

	public static String simpleFormat(Date d) {
		return sdf.format(d);
	}

	/**
	 * 得到天信息
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static int getDayOfMonth(Date date) {
		utilCalendar.setTime(date);
		return utilCalendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 得到月信息
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static int getMonth(Date date) {
		utilCalendar.setTime(date);
		return utilCalendar.get(Calendar.MONTH);
	}

	/**
	 * 得到年信息
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static int getYear(Date date) {
		utilCalendar.setTime(date);
		return utilCalendar.get(Calendar.YEAR);
	}

	/**
	 * 得到Timestamp的24小时制的小时
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static int get24Hour(Timestamp time) {
		utilCalendar.setTime(time);
		return utilCalendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 得到Timestamp的12小时制的小时
	 * 
	 * @param time
	 * @return
	 */
	public synchronized static int get12Hour(Timestamp time) {
		utilCalendar.setTime(time);
		return utilCalendar.get(Calendar.HOUR);
	}

	/**
	 * 得到Timestamp的分钟
	 * 
	 * @param time
	 * @return
	 */
	public synchronized static int getMinute(Timestamp time) {
		utilCalendar.setTime(time);
		return utilCalendar.get(Calendar.MINUTE);
	}

	/**
	 * 得到指定日期为周几
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static int getWeekDay(Date date) {
		utilCalendar.setTime(date);
		return utilCalendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 将时间转变成字符串 DEFAULT_DATE_FORMAT yyyy-MM-dd
	 * 
	 * @param calendar
	 * @return
	 */
	public static String convertDateToString(Calendar calendar) {
		return convertToString(DEFAULT_DATE_FORMAT, calendar);
	}

	/**
	 * 将时间转变成字符串 DEFAULT_TIME_FORMAT : yyyy-MM-dd HH:mm:ss
	 * 
	 * @param calendar
	 * @return
	 */
	public static String convertTimeToString(Calendar calendar) {
		return convertToString(DEFAULT_TIME_FORMAT, calendar);
	}

	/**
	 * 将时间转变成字符串
	 * 
	 * @param mask
	 * @param calendar
	 * @return
	 */
	public static String convertToString(String mask, Calendar calendar) {
		return convertToString(mask, calendar.getTime());
	}

	/**
	 * 将时间转变成字符串 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
	 * 
	 * @param strCalendar
	 * @return
	 */
	public static Calendar convertStringToCalendar(String strCalendar) throws ParseException {
		Calendar calendar = null;
		try {
			calendar = convertToCalendar(DEFAULT_TIME_FORMAT, strCalendar);
		} catch (ParseException e) {
			try {
				calendar = convertToCalendar(DEFAULT_DATE_FORMAT, strCalendar);
			} catch (ParseException ex) {
				throw e;
				// throw new ParseException("非时间表达式：" +
				// strCalendar + " . 正确格式：yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd");
			}
		}
		return calendar;
	}

	/**
	 * 根据指定的格式，将字符串转变成时间变量
	 * 
	 * @param mask:字符串的格式
	 * @param strCalendar
	 * @return
	 * @throws ParseException
	 */
	public static Calendar convertToCalendar(String mask, String strCalendar) throws ParseException {
		return getCalendar(convertToDate(mask, strCalendar));
	}

	/**
	 * 将时间转变成字符串 DEFAULT_TIME_FORMAT : yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String convertTimeToString(Date date) {
		return convertToString(DEFAULT_TIME_FORMAT, date);
	}

	/**
	 * 使用默认的时间格式 DEFAULT_DATE_FORMAT yyyy-MM-dd ，格式化时间
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDateToString(Date date) {
		return convertToString(DEFAULT_DATE_FORMAT, date);
	}

	/**
	 * 将字符串转变成时间变量
	 * 
	 * @param mask:时间格式
	 * @param date
	 * @return
	 */
	public static String convertToString(String mask, Date date) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (date == null)
			throw new NullPointerException("参数 时间 不能为null");

		if (mask == null)
			throw new NullPointerException("参数 时间 格式 不能为null");
		// SimpleDateFormat自然会去检测
		df = new SimpleDateFormat(mask);
		returnValue = df.format(date);

		return (returnValue);
	}

	/**
	 * 将字符串转变成时间变量 DEFAULT_DATE_FORMAT : yyyy-mm-dd
	 * 
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToDate(String strDate) throws ParseException {
		return convertToDate(DEFAULT_DATE_FORMAT, strDate);
	}

	/**
	 * 将字符串转变成时间变量 DEFAULT_TIME_FORMAT : yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strTime
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToTime(String strTime) throws ParseException {
		return convertToDate(DEFAULT_TIME_FORMAT, strTime);
	}

	/**
	 * 根据指定的格式，将字符串转变成时间变量
	 * 
	 * @param mask
	 *            字符串的格式
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date convertToDate(String mask, String strDate) throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;

		if (strDate == null)
			throw new NullPointerException("参数 时间 不能为null");

		if (mask == null)
			throw new NullPointerException("参数 时间 格式 不能为null");

		df = new SimpleDateFormat(mask);
		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			throw pe;
		}

		return date;
	}

	/**
	 * 得到指定Calendar的Timestamp
	 * 
	 * @param cal
	 * @return
	 */
	static public Timestamp getTimestamp(Calendar cal) {
		return new Timestamp(getTimeInMillis(cal));
	}

	private static long getTimeInMillis(Calendar cal) {
		return cal.getTime().getTime();
	}

	/**
	 * 得到指定Calendar的Date
	 * 
	 * @param cal
	 * @return
	 */
	static public Date getDate(Calendar cal) {
		return cal.getTime();
	}

	/**
	 * 得到指定Date的Timestamp
	 * 
	 * @param date
	 * @return
	 */
	static public Timestamp getTimestamp(Date date) {
		long lTime = date.getTime();
		return new Timestamp(lTime);
	}

	/**
	 * 得到指定Date的Calendar
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	/**
	 * 得到指定Timestamp的Date
	 * 
	 * @param time
	 * @return
	 */
	static public Date getDate(Timestamp time) {
		long lTime = time.getTime();
		return new Date(lTime);
	}

	/**
	 * 比较是否两个Date是同一天，开销挺大的
	 * 
	 * @param oneDate
	 * @param otherDate
	 * @return
	 */
	public static boolean isSameDate(Date oneDate, Date otherDate) {
		return isSameDate(getCalendar(oneDate), getCalendar(otherDate));
	}

	/**
	 * 比较两Calendar是否是同一天
	 * 
	 * @param oneDate
	 * @param otherDate
	 * @return
	 */
	public static boolean isSameDate(Calendar oneDate, Calendar otherDate) {
		if ((oneDate.get(Calendar.YEAR) == otherDate.get(Calendar.YEAR)) &&
		// ** 不确定是否更高效
				// (oneDate.get(Calendar.MONTH) ==
				// otherDate.get(Calendar.MONTH)) &&
				// (oneDate.get(Calendar.DATE) ==
				// otherDate.get(Calendar.DATE)))
				(oneDate.get(Calendar.DAY_OF_YEAR) == otherDate.get(Calendar.DAY_OF_YEAR)))
			return true;
		else
			return false;
	}

	/**
	 * 得到当前时间sendTime是否是节假日的时间
	 * @param startDate
	 * @param workDays
	 * @return
	 * @throws Exception 
	 */
	public static Date getNotHolidays(Date date) throws Exception{
		Object jdbcTemplateObj = BeanFactory.getBean("jdbcTemplate");
		if(jdbcTemplateObj!=null){
			JdbcTemplate jdbcTemplate = (JdbcTemplate) jdbcTemplateObj;
			String sql = "select PKG_quick.f_get_waitSendTime(?) from dual";
			Date notHolidays = (Date) jdbcTemplate.queryForObject(sql, new Object[] { date }, Date.class);
			return notHolidays;
		}else{
			return date;
		}
		
	}
	public static void main(String[] args) throws Exception {
       Date date = DateUtil.dateAddSub(new Date(), 0, 1, 1);
       System.out.println(date);

    }

	public static Date dateAddSub(Date date,int year,int month,int day) throws Exception {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.YEAR,year);//日期减1年
        rightNow.add(Calendar.MONTH,month);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,day);//日期加10天
        Date dt1=rightNow.getTime();
        return dt1;
	}
}